//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2005 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)


using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace SoapTrace.Core.Formatters
{
	/// <summary>
	/// Formats a http header.
	/// </summary>
	public class HttpHeaderFormatter
	{
		/// <summary>
		/// The field delimiter colour.
		/// </summary>
		private Color m_FieldDelimiterColour = Color.Blue;
		
		/// <summary>
		/// The field name colour.
		/// </summary>
		private Color m_FieldNameColour = Color.FromArgb( 153, 0, 0 );
		
		/// <summary>
		/// The field value colour.
		/// </summary>
		private Color m_FieldValueColour = Color.Black;
		
		/// <summary>
		/// New line character length.
		/// </summary>
		private const int NewLineLength = 2;

		/// <summary>
		/// Creates a new instance of the <see cref="HttpHeaderFormatter"/>
		/// class.
		/// </summary>
		public HttpHeaderFormatter( )
		{
		}
		
		/// <summary>
		/// Gets or sets the text colour to use for field delimiters.
		/// </summary>
		public Color FieldDelimiterColour
		{
			get
			{
				return m_FieldDelimiterColour;
			}
			
			set
			{
				m_FieldDelimiterColour = value;
			}
		}
	
		/// <summary>
		/// Gets or sets the text colour to use for field names. 
		/// </summary>
		public Color FieldNameColour
		{
			get
			{
				return m_FieldNameColour;
			}
			
			set
			{
				m_FieldNameColour = value;
			}
		}	
		
		/// <summary>
		/// Gets or sets the text colour to use for field values. 
		/// </summary>
		public Color FieldValueColour
		{
			get
			{
				return m_FieldValueColour;
			}
			
			set
			{
				m_FieldValueColour = value;
			}
		}						
		
		
		/// <summary>
		/// Formats a string that contains Http Header text.
		/// </summary>
		public FormattedText Format( string text )
		{			
			FormattedText FormattedText = new FormattedText( m_FieldValueColour );
			FormattedText.Append( text );
						
			string[ ] Fields = text.Replace( "\r\n", "\n" ).Split( '\n' );

			if( Fields.Length > 0 )
			{
				// Ignore first line.
				int CharIndex = Fields[ 0 ].Length + 2;

				for( int i = 1; i < Fields.Length; ++i )
				{
					string Field = Fields[ i ];
					
					if( Field.Length > 0 )
					{
						int DelimiterIndex = Field.IndexOf( ':' );
												
						if( DelimiterIndex != - 1 )
						{
							FormattedText.SetColour( m_FieldNameColour, CharIndex, DelimiterIndex );
							
							FormattedText.SetColour( m_FieldDelimiterColour, CharIndex + DelimiterIndex, 1 );
						}
					}
					
					CharIndex += Field.Length + 2; 
				}
			}
			
			return FormattedText;
		}
	}
}
